package com.servlets;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.displaytag.tags.TableTagParameters;
import org.displaytag.util.ParamEncoder;

import com.guestbook.dao.GuestBookDAO;
import com.guestbook.dao.impl.GuestBookDAOJdbc;
import com.guestbook.javabeans.GuestBook;
import com.tools.OracleUtil;
import com.tools.Pagination;

public class ManageServlet extends HttpServlet {

	private static final long serialVersionUID = -3291346059893456200L;
	OracleUtil db = null;
	private int pageSize = 10;

	public void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		GuestBookDAO dao = new GuestBookDAOJdbc();
		GuestBook gb = dao.getGuestBook(Integer.parseInt(request
				.getParameter("id")));
		request.setAttribute("guestbook.admin.edit", gb);
		request.getRequestDispatcher("/admin/secure/edit.jsp").forward(request,
				response);
	}

	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		GuestBookDAO dao = new GuestBookDAOJdbc();
		GuestBook gb = new GuestBook();
		gb.setId(new Integer(request.getParameter("id")));
		gb.setName(request.getParameter("name"));
		gb.setContent(request.getParameter("content"));
		gb.setEmail(request.getParameter("email"));
		gb.setPhone(request.getParameter("phone"));
		gb.setTitle(request.getParameter("title"));
		boolean result = dao.update(gb);
		if (result) {
			request.setAttribute("guestbook.admin.update.message", "留言更新成功！");
			request.setAttribute("guestbook.admin.edit", gb);
		} else {
			request.setAttribute("guestbook.admin.update.message", "更新不成功！");
		}
		request.getRequestDispatcher("/admin/secure/updateResult.jsp").forward(
				request, response);
	}

	public void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		GuestBookDAO dao = new GuestBookDAOJdbc();
		boolean result = dao.delete(new Integer(request.getParameter("id")));
		if (result) {
			request.setAttribute("guestbook.admin.delete.message", "删除成功！");
		} else {
			request.setAttribute("guestbook.admin.delete.message", "删除不成功！");
		}
		request.getRequestDispatcher("/admin/secure/deleteResult.jsp").forward(
				request, response);
	}

	public void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageNumber;
		String paramName = (new ParamEncoder("gb")
				.encodeParameterName(TableTagParameters.PARAMETER_PAGE));// displaytag
		try {
			pageNumber = Integer.parseInt(request.getParameter(paramName));
		} catch (Exception e) {
			pageNumber = 1;
		}
		GuestBookDAO dao = new GuestBookDAOJdbc();
		Pagination pager = dao.getGuestBookByPage(pageSize, pageNumber);

		request.setAttribute("guestbook.admin.list", pager.getList());
		request.setAttribute("guestbook.admin.total", pager.getMaxElements());
		request.getRequestDispatcher("/admin/secure/list.jsp").forward(request,
				response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		db = new OracleUtil("java:/comp/env/jdbc/oracleds");
		db.init();
		String method = request.getParameter("q");
		if (method == null) {
			method = "list";
		}
		if (method.equals("edit")) {
			edit(request, response);
		} else if (method.equals("delete")) {
			delete(request, response);
		} else if (method.equals("update")) {
			update(request, response);
		} else {
			list(request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void init(ServletConfig config) throws ServletException {
		super.init();
		try {
			this.pageSize = Integer.parseInt(config.getServletContext()
					.getInitParameter("pageSize"));
		} catch (Exception e) {
			this.pageSize = 10;
		}
	}
}
